package com.mmlc.businessutils;

import android.widget.ListView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.meimei.lichengkongjian.Adapter.AllOrderAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/7/23 0023
 * .
 */
public class PullToRefreshUtils {
    public MyRefreshListener myRefreshListener;
    PullToRefreshListView pullRefreshList;

    public <T> PullToRefreshUtils(PullToRefreshListView pullRefreshList,RefreshListener<T> rf){
        this.pullRefreshList = pullRefreshList;
        this.rf = rf;
        initListView();
    }
    public <T> void recordRequest(BaseRequestFilterLayer filter,boolean isSuccess){
        if(isSuccess){//请求成功
            pullRefreshList.onRefreshComplete();

            List requestList = rf.getRequestList();
            List adapterList = rf.getAdapterList();

            if(requestList.size() > 0 ){
                if (isPUll){
                    if(adapterList.size() > 0){
                        boolean compareFirst = rf.compareFirstItem((T) adapterList.get(0));
                        if(!compareFirst){
                            rf.getAdapterList().addAll(0,requestList);
                        }
                    }else{
                        adapterList.addAll(requestList);
                    }
                }else{
                    isNeedIncrement = true;
                    adapterList.addAll(requestList);
                }

                rf.processList(rf.getAdapterList());
            }else{
                if(adapterList.size() == 0){
                    filter.AddNoDataUI();
                }
                isNeedIncrement = false;
                return;
            }


        }else{//请求失败
            pullRefreshList.onRefreshComplete();
            isNeedIncrement = false;
        }
    }

    public interface  RefreshListener<T>{
        public   boolean compareFirstItem(T adapterFirstItem);
        public   void processList(List<T> list);
        public   void refresh(boolean isPull);
        public  List<T> getAdapterList();
        public  List<T> getRequestList();
    }
    public RefreshListener rf;
    public void setRefreshListener(RefreshListener rf){
        this.rf = rf;
    }
    private void initListView() {
        pullRefreshList.setMode(PullToRefreshBase.Mode.BOTH);
        myRefreshListener = new MyRefreshListener();
        pullRefreshList.setOnRefreshListener(myRefreshListener);
    }


    private int pageNow = 0;
    public int pageSize = 10;
    public int currPageIndex;
    boolean isPUll = true;
    boolean isNeedIncrement = true;
    public AllOrderAdapter orderListAdapter;
    public int requestType = -1;
    public class MyRefreshListener implements PullToRefreshBase.OnRefreshListener2<ListView> {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            isPUll = true;
            currPageIndex = 0;
            rf.refresh(true);
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            isPUll = false;
            if(isNeedIncrement){
                pageNow++;
            }
            currPageIndex = pageNow;
            rf.refresh(false);
        }
    }

}
